//
//  TZYHeaderWebViewController.m
//  Moment
//
//  Created by dllo on 15/12/30.
//  Copyright © 2015年 片刻. All rights reserved.
//

#import "TZYHeaderWebViewController.h"
#import "TZYHeaderView.h"
#import "TZYHeaderCell.h"

#define buttonHeight ((SCREEN_HEIGHT - 64 - 49) / 3) / 4

@interface TZYHeaderWebViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UIView *headView;
@property (nonatomic, retain) UIImageView *photoV;
@property (nonatomic, retain) UILabel *nameL;
@property (nonatomic, retain) UILabel *qianL;
@property (nonatomic, retain) UIButton *down;
@property (nonatomic, retain) UIImageView *imageH;
@property (nonatomic, retain) UIView *downV;
@property (nonatomic, retain) UIScrollView *scrollV;
@property (nonatomic, retain) NSDictionary *dic;
@property (nonatomic, retain) UIImageView *imageXB;
@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, assign) NSInteger i;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) NSMutableDictionary *typeDic;
@property (nonatomic, retain) NSMutableArray *resultArray;
@property (nonatomic, retain) UIView *nilV;
@property (nonatomic, strong) TZYHeaderView *headerView;
@property (nonatomic, assign) BOOL isSelected;
@property (nonatomic, strong) UIButton *button;


@end

@implementation TZYHeaderWebViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.myTableView reloadData];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [self getData];
    [self createHeaderView];
    [self createTableView];
    
    //创建View
    self.headerView = [[TZYHeaderView alloc] initWithFrame:CGRectMake(0, 150 * HEIGHT + 35, SCREEN_WIDTH, SCREEN_HEIGHT / 4)];
    [self.view addSubview:self.headerView];
    [self.view bringSubviewToFront:self.headerView];
    self.headerView.hidden = YES;
    
    UIImage *image = [UIImage imageNamed:@"back.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
}


- (void)getData
{
    NSString *urlStr = @"http://api2.pianke.me/profile/info";
    
    NSString *para = [NSString stringWithFormat:@"auth=W5dqQCm159v1DC8hyHcbrkDJxQMD6Z7bep29tWTYzcWP22NFOSRpU6m3k&client=1&deviceid=62F2D81E-5D3B-44AB-AAB9-16B19D06BB21&uid=%@&version=3.0.6", self.str];
    
    
    [GetData postUrl:urlStr body:para result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        
        self.dic = [NSDictionary dictionary];
        self.typeDic = [NSMutableDictionary dictionary];
        self.dic =  [(NSDictionary *)result objectForKey:@"data"];
        NSDictionary *arrUse = [NSDictionary dictionary];
        arrUse = [self.dic valueForKey:@"userinfo"];
        [self.imageH sd_setImageWithURL:[NSURL URLWithString:[arrUse valueForKey:@"coverimg"]]placeholderImage:[UIImage imageNamed:@"150113122411-1.jpg"]];
        
       
        
        //给头部赋值
        [self.photoV sd_setImageWithURL:[NSURL URLWithString:[arrUse valueForKey:@"icon"]] placeholderImage:[UIImage imageNamed:@"150113122411-1.jpg"]];
        self.nameL.text = [arrUse valueForKey:@"uname"];
        
        
        self.nameL.textColor = [UIColor whiteColor];
        self.nameL.font = [UIFont boldSystemFontOfSize:[TZYFontSize getFontSizeForBig]];
        
        self.qianL.text = [arrUse valueForKey:@"desc"];
        self.qianL.textColor = [UIColor whiteColor];
        self.qianL.font = [UIFont boldSystemFontOfSize:[TZYFontSize getFontSizeForSmall]];
        
        
        
        self.arr = [NSMutableArray array];
        self.arr  = [self.dic valueForKey:@"list"];
        self.resultArray = self.arr;
        [self.typeDic setObject:[NSMutableArray array] forKey:@"2"];
        [self.typeDic setObject:[NSMutableArray array] forKey:@"15"];
        [self.typeDic setObject:[NSMutableArray array] forKey:@"24"];
        [self.typeDic setObject:[NSMutableArray array] forKey:@"17"];
        
        for (NSDictionary *dic in self.arr) {
            for (NSString *key in self.typeDic) {
                if ([key isEqualToString:[NSString stringWithFormat:@"%@", [dic valueForKey:@"type"]]]) {
                    [[self.typeDic valueForKey:key] addObject:dic];
                    NSLog(@"%@", key);
                }
            }
            
        }
        
        
        [self.myTableView reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];

}


- (void)backAction
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}


- (void)createHeaderView
{
    
    self.imageH = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
    self.imageH.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:self.imageH];
    
    UIButton *buttonBack = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonBack.frame = CGRectMake(10 * WIDTH, 25 * HEIGHT, 30 * WIDTH, 30 * HEIGHT);
    [self.view addSubview:buttonBack];
    [buttonBack setImage:[UIImage imageNamed:@"iconfont-fanhui-3.png"] forState:UIControlStateNormal];
    [buttonBack addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.photoV = [[UIImageView alloc] initWithFrame:CGRectMake(60 * WIDTH, 50 * HEIGHT, 60 * WIDTH, 60 * HEIGHT)];
    //self.photoV.backgroundColor = [UIColor cyanColor];
    CALayer *layer = self.photoV.layer;
    [layer setMasksToBounds:YES];
    [layer setCornerRadius:30];
    [self.view addSubview:self.photoV];
   
    
    self.nameL = [[UILabel alloc] initWithFrame:CGRectMake(140 * WIDTH, 55 * HEIGHT, 200 * WIDTH, 30 * HEIGHT)];
    self.nameL.textColor = [UIColor blueColor];
    self.nameL.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:self.nameL];
    
    
    self.qianL = [[UILabel alloc] initWithFrame:CGRectMake(140 * WIDTH, 80 * HEIGHT , 200 * WIDTH, 30 * HEIGHT)];
    self.qianL.textColor = [UIColor purpleColor];
    
    self.qianL.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:self.qianL];

    

    
    
}


#pragma mark-创建tableView
- (void)createTableView
{
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 150 * HEIGHT, self.view.frame.size.width, self.view.frame.size.height - 64 * HEIGHT) style:UITableViewStylePlain];
    self.myTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    //self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.showsVerticalScrollIndicator = NO;
    self.myTableView.backgroundColor = [UIColor clearColor];
    //self.myTableView.sectionHeaderHeight = 40 * HEIGHT;

    
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.myTableView registerClass:[TZYHeaderCell class] forCellReuseIdentifier:@"TZYHeaderCell"];

    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.resultArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"TZYHeaderCell";
    TZYHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    
    NSDictionary *dic = nil;
    NSString *str = nil;
    if (self.resultArray.count > indexPath.row) {
        dic = [self.resultArray objectAtIndex:indexPath.row];
        str = [NSString stringWithFormat:@"%@",[dic valueForKey:@"type"]];
    }

    
    
    [cell.imageB sd_setImageWithURL:[NSURL URLWithString:[dic valueForKey:@"coverimg"]] placeholderImage:[UIImage imageNamed:@"占位正.jpg"]];
    cell.titleL.text = [dic valueForKey:@"title"];
    cell.timeL.text = [dic valueForKey:@"addtime_f"];
    if ([str isEqualToString:@"15"]) {
        
        cell.typeL.text = @"发布一个文章";
        
    } else if([str isEqualToString:@"24"]) {
        
        cell.typeL.text = @"发布一个话题";
    }
    cell.wenzhangL.text = [dic valueForKey:@"content"];
    
    cell.commentLabel.text = [NSString stringWithFormat:@"%@",[[dic valueForKey:@"counterList"] valueForKey:@"comment"]];
    cell.likeLabel.text = [NSString stringWithFormat:@"%@", [[dic valueForKey:@"counterList"] valueForKey:@"like"]];
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HeaderCellHeight;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 35;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    
    UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
    sectionView.backgroundColor = [UIColor whiteColor];
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(SCREEN_WIDTH / 2 - 10, 5, 25, 25);
    [sectionView addSubview:self.button];
    
    [self.button addTarget:self action:@selector(momentAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.isSelected = NO;
    
    [self.button setBackgroundImage:[UIImage imageNamed:@"iconfont-xiangshang.png"] forState:UIControlStateNormal];
    
    
    return sectionView;
}



- (void)momentAction:(UIButton *)sender
{
    if (!self.isSelected) {
        self.headerView.hidden = NO;
        [self.button setBackgroundImage:[UIImage imageNamed:@"iconfont-1.png"] forState:UIControlStateNormal];
        
    } else {
        self.headerView.hidden = YES;
        [self.button setBackgroundImage:[UIImage imageNamed:@"iconfont-xiangshang.png"] forState:UIControlStateNormal];
    }
    
    self.isSelected = !self.isSelected;
    
}







- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y = self.myTableView.contentOffset.y;
    if (y < 0) {
        CGRect frame = self.imageH.frame;
        frame.origin.y = y;
        frame.size.height = -y + 300 * HEIGHT;
        self.imageH.frame = frame;
    }

    self.headerView.hidden = YES;
    [self.button setBackgroundImage:[UIImage imageNamed:@"iconfont-xiangshang.png"] forState:UIControlStateNormal];
    self.isSelected = NO;
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.headerView.hidden = YES;
    [self.button setBackgroundImage:[UIImage imageNamed:@"iconfont-xiangshang.png"] forState:UIControlStateNormal];
    self.isSelected = NO;
}




- (void)leftButtonAction:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
